import Vue from 'vue'
import Router from 'vue-router'
import navAry from './nav.config'
const packageJson = require('../package.json')
const isProd = process.env.NODE_ENV === 'production'
const publicPath = isProd ? `${packageJson.publicPath}` : ''

Vue.use(Router)

const routes = [
  {
    path: '/',
    redirect: {
      path: '/introduce',
    },
  },
  {
    path: '/introduce',
    component: () => import(`$exp/views/Introduce.vue`),
  },
  {
    path: '/guide',
    redirect: {
      path: '/guide/installation',
    },
  },
  {
    path: '/resource',
    component: () => import(`$exp/views/Resource.vue`),
  },
  {
    path: '/component',
    redirect: {
      path: '/component/typography',
    },
  },
]

const routeRegister = (context, ary) => {
  ary.forEach((nav) => {
    const { path, children } = nav
    if (path) {
      routes.push({
        path: `/${context}${path}`,
        component: nav.component
          ? nav.component
          : () => import(`$exp/docs${nav.path}.md`),
        meta: { icon: nav.icon },
      })
    }
    if (children && children.length > 0) {
      routeRegister(context, children)
    }
  })
}

routeRegister('guide', navAry.guide)
routeRegister('component', navAry.component)

var router = new Router({
  mode: 'hash', // hash  history
  base: publicPath,
  routes,
})

export default router
